package gov.pr.celepar.sentinela.admin.action;

import java.util.Collection;

import gov.pr.celepar.sentinela.admin.dao.FuncaoDAO;
import gov.pr.celepar.sentinela.admin.dao.IconeFuncaoDAO;
import gov.pr.celepar.sentinela.admin.dao.factory.DAOFactoryServer;
import gov.pr.celepar.sentinela.admin.excecao.SentinelaServerException;
import gov.pr.celepar.sentinela.admin.form.IconeForm;
import gov.pr.celepar.sentinela.admin.pojo.Funcao;
import gov.pr.celepar.sentinela.admin.pojo.Icone;
import gov.pr.celepar.sentinela.admin.pojo.IconeSistema;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.util.MessageResources;

public class IconeAction extends BaseAction {

	private final String TROCAR_ORDEM = "trocar_ordem";

	public String executar(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		this.forward = "lista_icone";
		String pag = this.forward;
		try {
			IconeForm iconeForm = (IconeForm) form;

			if (this.INCLUIR.equalsIgnoreCase(iconeForm.getAcao())) {
				this.comunicacao.autorizaOperacao('I', request, response);
				this.listarSistemasAutorizados(request);
				if (iconeForm.getCodSistema() != null
						&& !iconeForm.getCodSistema().equals("")) {
					DAOFactoryServer daof = DAOFactoryServer.getDAOFactory();
					FuncaoDAO bf = daof.getFuncaoDAO();
					IconeFuncaoDAO bi = daof.getIconeFuncaoDAO();

					Collection funcoes = bf.getArvore(Long.decode(iconeForm
							.getCodSistema()));
					request.setAttribute("listaFuncoes", funcoes);
					Collection funcoesGenericas = bf.getFuncoesGenericas(Long
							.decode(iconeForm.getCodSistema()));
					request.setAttribute("listaFuncoesGenericas",
							funcoesGenericas);
					Collection icones = bi.listarIcones();
					request.setAttribute("listaIcones", icones);
				}
				pag = "inc_icone";
			} else if (this.EXCLUIR.equalsIgnoreCase(iconeForm.getAcao())) {
				this.comunicacao.autorizaOperacao('E', request, response);
				pag = excluir(iconeForm);
				MessageResources messages = getResources(request);
				String msg = messages.getMessage("sucesso.icone.excluir");
				this.addMessageSucess(msg, request);
			} else if (this.TROCAR_ORDEM.equalsIgnoreCase(iconeForm.getAcao())) {
				this.comunicacao.autorizaOperacao('A', request, response);
				pag = ordenar(iconeForm);
			} else if (this.CONF_INCLUIR.equalsIgnoreCase(iconeForm.getAcao())) {
				this.comunicacao.autorizaOperacao('I', request, response);
				pag = incluir(iconeForm);
				MessageResources messages = getResources(request);
				String msg = messages.getMessage("sucesso.icone.incluir");
				this.addMessageSucess(msg, request);
			}
		} catch (SentinelaServerException appEx) {
			throw appEx;
		} catch (Exception e) {
			throw e;
		}
		return pag;
	}

	private String incluir(IconeForm form) throws Exception {
		try {
			DAOFactoryServer daof = DAOFactoryServer.getDAOFactory();
			FuncaoDAO bf = daof.getFuncaoDAO();
			IconeFuncaoDAO bif = daof.getIconeFuncaoDAO();
			Funcao funcao = bf.consultar(Long.decode(form.getCodSistema()),
					Long.decode(form.getCodFuncao()));
			Icone icone = bif
					.consultarIcone(Integer.decode(form.getCodIcone()));

			IconeSistema iconeFuncao = new IconeSistema();
			iconeFuncao.setFuncao(funcao);
			iconeFuncao.setIcone(icone);

			bif.incluir(iconeFuncao);
		} catch (Exception e) {
			this.forward = "erro_inc_icone";
			throw e;
		}

		return "sucesso_inc_icone";
	}

	private String excluir(IconeForm form) throws Exception {

		try {
			DAOFactoryServer daof = DAOFactoryServer.getDAOFactory();
			FuncaoDAO bf = daof.getFuncaoDAO();
			IconeFuncaoDAO bif = daof.getIconeFuncaoDAO();
			Funcao funcao = bf.consultar(Long.decode(form.getCodSistema()),
					Long.decode(form.getCodFuncao()));
			Icone icone = bif
					.consultarIcone(Integer.decode(form.getCodIcone()));

			IconeSistema iconeFuncao = new IconeSistema();
			iconeFuncao.setFuncao(funcao);
			iconeFuncao.setIcone(icone);

			bif.excluir(iconeFuncao);
		} catch (Exception e) {
			throw e;
		}

		return "exc_icone";
	}

	public String ordenar(IconeForm form) throws Exception {
		try {
			DAOFactoryServer daof = DAOFactoryServer.getDAOFactory();
			IconeFuncaoDAO bif = daof.getIconeFuncaoDAO();

			IconeSistema iconeFuncao = bif.consultar(Long.decode(form
					.getCodFuncao()), Long.decode(form.getCodSistema()),
					Integer.decode(form.getCodIcone()));

			if (form.isAnterior()) {
				IconeSistema iconeFuncaoAnterior = bif
						.consultarAnterior(iconeFuncao);
				bif.trocarOrdem(iconeFuncao, iconeFuncaoAnterior);
			} else if (form.isPosterior()) {
				IconeSistema iconeFuncaoPosterior = bif
						.consultarPosterior(iconeFuncao);
				bif.trocarOrdem(iconeFuncao, iconeFuncaoPosterior);
			}
		} catch (Exception e) {
			throw e;
		}

		return "ordena_icone";
	}
}
